package com.lanou.tipsy.fragment.songsList;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.songList.SongListMenuAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.entity.songList.songslist.ChildrenEntity;
import com.lanou.tipsy.entity.songList.songslist.RankDataEntity;
import com.lanou.tipsy.entity.songList.songslist.SongRank;
import com.lanou.tipsy.tools.listview.NoScrollGridView;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/31.
 *
 *   歌单 分类菜单 Fragment (暂时不用  误删)
 *
 *
 */
public class SongListMenuFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private SongListMenuAdapter adapter001,adapter002,adapter003,adapter004,adapter005;
    private NoScrollGridView gridView1,gridView2,gridView3,gridView4,gridView5;
    private TextView textView01,textView02,textView03,textView04,textView05,allListTv,cancelTv;
    private String url = "http://api.dongting.com/song/tag/songlist";
    private RequestQueue requestQueue;
    private ArrayList<RankDataEntity> rankDataEntity;
    private ArrayList<ChildrenEntity> childrenEntity1,childrenEntity2,childrenEntity3,childrenEntity4,childrenEntity5;
    private String tag1,tag2,tag3,tag4,tag5;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_songlist_menu,null);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        initNetWork();
    }

    public void init() {
        gridView1 = (NoScrollGridView) getView().findViewById(R.id.songListGV_1);
        gridView2 = (NoScrollGridView) getView().findViewById(R.id.songListGV_2);
        gridView3 = (NoScrollGridView) getView().findViewById(R.id.songListGV_3);
        gridView4 = (NoScrollGridView) getView().findViewById(R.id.songListGV_4);
        gridView5 = (NoScrollGridView) getView().findViewById(R.id.songListGV_5);
        textView01 = (TextView) getView().findViewById(R.id.text01);
        textView02 = (TextView) getView().findViewById(R.id.text02);
        textView03 = (TextView) getView().findViewById(R.id.text03);
        textView04 = (TextView) getView().findViewById(R.id.text04);
        textView05 = (TextView) getView().findViewById(R.id.text05);

        //allListTv = (TextView) getView().findViewById(R.id.allListTv);
        cancelTv = (TextView) getView().findViewById(R.id.cancelTv);

        gridView1.setOnItemClickListener(this);
        gridView2.setOnItemClickListener(this);
        gridView3.setOnItemClickListener(this);
        gridView4.setOnItemClickListener(this);
        gridView5.setOnItemClickListener(this);

        allListTv.setOnClickListener(this);
        cancelTv.setOnClickListener(this);
    }

    public void initAdapter(){
        adapter001 = new SongListMenuAdapter(getActivity(),childrenEntity1);
        gridView1 = (NoScrollGridView) getView().findViewById(R.id.songListGV_1);
        gridView1.setAdapter(adapter001);

        adapter002 = new SongListMenuAdapter(getActivity(),childrenEntity2);
        gridView2 = (NoScrollGridView) getView().findViewById(R.id.songListGV_2);
        gridView2.setAdapter(adapter002);

        adapter003 = new SongListMenuAdapter(getActivity(),childrenEntity3);
        gridView3 = (NoScrollGridView) getView().findViewById(R.id.songListGV_3);
        gridView3.setAdapter(adapter003);

        adapter004 = new SongListMenuAdapter(getActivity(),childrenEntity4);
        gridView4 = (NoScrollGridView) getView().findViewById(R.id.songListGV_4);
        gridView4.setAdapter(adapter004);

        adapter005 = new SongListMenuAdapter(getActivity(),childrenEntity5);
        gridView5 = (NoScrollGridView) getView().findViewById(R.id.songListGV_5);
        gridView5.setAdapter(adapter005);
    }
    public void initData(){
        childrenEntity1 = rankDataEntity.get(0).getChildren();
        childrenEntity2 = rankDataEntity.get(1).getChildren();
        childrenEntity3 = rankDataEntity.get(2).getChildren();
        childrenEntity4 = rankDataEntity.get(3).getChildren();
        childrenEntity5 = rankDataEntity.get(4).getChildren();

        tag1 = rankDataEntity.get(0).getTag();
        tag2 = rankDataEntity.get(1).getTag();
        tag3 = rankDataEntity.get(2).getTag();
        tag4 = rankDataEntity.get(3).getTag();
        tag5 = rankDataEntity.get(4).getTag();

        textView01.setText(tag1);
        textView02.setText(tag2);
        textView03.setText(tag3);
        textView04.setText(tag4);
        textView05.setText(tag5);
    }
    public void initNetWork(){
        requestQueue = Volley.newRequestQueue(getActivity());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null) {
                    Gson gson = new Gson();
                    String str = response.toString();
                    //songRank = new SongListData();
                    SongRank songRank = gson.fromJson(str, SongRank.class);
                    rankDataEntity = songRank.getData();
                    initData();
                    initAdapter();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ChildrenEntity childrenEntity = (ChildrenEntity) parent.getItemAtPosition(position);
        String tag = childrenEntity.getTag();
        int intTtagId = childrenEntity.getTagID();
        String tagId = intTtagId + "";
        Log.i("test","SongListMenuFragment" +  tag + " " + tagId);

        FragmentManager manager = getActivity().getSupportFragmentManager();
        SongsListsFragment fragment = new SongsListsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tag",tag);
        bundle.putString("tagId",tagId);
        fragment.setArguments(bundle);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.fl_main, fragment);
        transaction.commit();

    }

    @Override
    public void onClick(View v) {
       /* switch (v.getId()){
            case R.id.allListTv:
                break;
            case R.id.cancelTv:

                break;
        }*/
    }
}
